package com.automation.fitness.core.userinfo;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public class BMI {
    private static final double[] BMI_RANGE_COND = new double[]{18.5, 24, 28};

    public static BMI calculate(double heightCm, double weightKg) {
        return new BMI(weightKg / Math.pow(heightCm / 100, 2));
    }

    private final double factor;

    /**
     * 获取BMI对应的肥胖程度
     */
    public BMI_RANGE getRange() {
        for (int i = 0; i < BMI_RANGE_COND.length; i++) {
            if (this.factor < BMI_RANGE_COND[i]) {
                return BMI_RANGE.values()[i];
            }
        }
        return BMI_RANGE.OBESE;
    }

    @Override
    public String toString() {
        return String.format("BMI: %.2f, %s", factor, this.getRange().getDesc());
    }
}
